//
//  ZHTextField.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/11.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

enum ZHTextFieldStyle {
    case `default`
    case number
    case password
}

class ZHTextField: NSView {
    var isVisiblePwd:Bool = true {
        didSet {
            
        }
    }
    var radius:CGFloat = 0 {
        didSet {
            self.view.zh_addBorder(radius: radius, borderColor: boardColor, borderWidth: boardWidth)
        }
    }
    
    var boardColor:NSColor = NSColor.black {
        didSet {
            self.view.zh_addBorder(radius: radius, borderColor: boardColor, borderWidth: boardWidth)
        }
    }
    
    var boardWidth:CGFloat = 0.5 {
        didSet {
            self.view.zh_addBorder(radius: radius, borderColor: boardColor, borderWidth: boardWidth)
        }
    }
    
    var isOnlyNumber:Bool = false {
        didSet {
            if isOnlyNumber == true {
                //self.textField.formatter = ZHNumberFormatter()
            }else {
                //self.textField.formatter = Formatter.init()
            }
        }
    }
    
    var style:ZHTextFieldStyle = .default {
        didSet {
            switch style {
            case .number:
                self.textField.isHidden = false
                self.secureTextField.isHidden = true
                self.visibleBtn.isHidden = true
            case .default:
                self.textField.isHidden = false
                self.secureTextField.isHidden = true
                self.visibleBtn.isHidden = true
            case .password:
                self.textField.isHidden = true
                self.secureTextField.isHidden = false
                self.visibleBtn.isHidden = false
            }
        }
    }
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.zh_setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func zh_setupUI() {
        self.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.width.equalToSuperview().offset(-10)
            make.center.equalToSuperview()
        }
        
        self.addSubview(secureTextField)
        secureTextField.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.width.equalToSuperview().offset(-10)
            make.center.equalToSuperview()
        }
        
        self.addSubview(visibleBtn)
        visibleBtn.snp.makeConstraints { (make) in
            make.size.equalTo(visibleBtn.normalImage.size)
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
        }
        
        self.boardColor = color_gray(200)
        self.boardWidth = 0
        self.radius = 2
        self.isVisiblePwd = true
    }
    
    lazy var view: NSView = {
        let view = NSView()
        return view
    }()
    
    
    lazy var visibleBtn: ZHButton = {
        let btn = ZHButton.init(frame: NSRect.zero)
        btn.canSelected = true
        btn.normalImage = NSImage.imageNamed("icon_pwd_visible")!
        btn.selectedImage = NSImage.imageNamed("icon_pwd_novisible")!
        btn.target = self
        btn.action = #selector(visibleBtnAction(senber:))
        btn.isHidden = true
        return btn
    }()
    
    lazy var textField: BaseTextField = {
        let tef = BaseTextField.init(frame: NSRect.zero)
        tef.baseDelegate = self
        return tef
    }()
    
    lazy var secureTextField: BaseSecureTextField = {
        let tef = BaseSecureTextField.init(frame: NSRect.zero)
        tef.baseDelegate = self
        tef.isHidden = true
        return tef
    }()
    
    @objc func visibleBtnAction(senber:ZHButton) {
        self.isVisiblePwd = !self.isVisiblePwd
        if self.isVisiblePwd == false {
            self.textField.stringValue = self.secureTextField.stringValue
        }else {
            self.secureTextField.stringValue = self.textField.stringValue
        }
        self.secureTextField.isHidden = !self.isVisiblePwd
        self.textField.isHidden = self.isVisiblePwd
    }
    
    
    
}

extension ZHTextField:BaseTextFieldDelegate {
    func mouseDownTextFieldClicked(textField: NSTextField) {
        
    }
    
    
}

extension ZHTextField: NSTextFieldDelegate {
    
    func controlTextDidChange(_ obj: Notification) {
        if obj.object as? BaseTextField == self.textField {
            let counVale = self.textField.stringValue
            let regex = "^1[0-9]{10}$"
            if counVale.isMatchedBy(regex: regex) {
                self.textField.stringValue = counVale.replace(regex: regex, content: "12324")
            }else {
                self.textField.stringValue = counVale
            }
            
            
        }
    }
}

